package logic

import (
	"context"
	"fmt"
	"goods_srv/models/mysql"
	"goods_srv/models/redis"

	"goods_srv/goodssrv"
	"goods_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *goodssrv.UserLoginRequest) (*goodssrv.UserLoginResponse, error) {
	// todo: add your logic here and delete this line

	if redis.Rdb.Get(context.Background(), "smsCodeLogin"+in.Phone).Val() != in.Code {
		return nil, fmt.Errorf("短信验证码错误")
	}
	var user mysql.User
	err := user.FindUser(in.Phone)
	if err != nil {
		return nil, fmt.Errorf("该用户不存在")
	}
	return &goodssrv.UserLoginResponse{
		UserId: int64(user.ID),
	}, nil
}
